package com.woniu.tms.studentinfo.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.woniu.tms.studentinfo.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.ClassesExample;
import com.woniu.tms.entity.User;
import com.woniu.tms.entity.UserExample;
import com.woniu.tms.entity.UserExample.Criteria;
import com.woniu.tms.entity.UserRole;
import com.woniu.tms.entity.UserRoleExample;
import com.woniu.tms.mapper.ClassesMapper;
import com.woniu.tms.mapper.UserMapper;
import com.woniu.tms.mapper.UserRoleMapper;

@Service
public class StudentServiceImpl implements StudentInfoService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ClassesMapper classesMapper;
    @Autowired
    UserRoleMapper userRoleMapper;


    @Override
    public Map<String, Object> findByRnameLikeAndUid(String rnameLike, Integer uid, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUnameLike("%" + rnameLike + "%");

        ClassesExample classesExample = new ClassesExample();
        com.woniu.tms.entity.ClassesExample.Criteria criteria2 = classesExample.createCriteria();
        criteria2.andTeacheridEqualTo(uid);

        List<Classes> classesList = classesMapper.selectByExample(classesExample);
        List<Integer> classIdList = new ArrayList<Integer>();
        for (Classes id : classesList) {
            classIdList.add(id.getCid());
        }


        criteria.andClassidIn(classIdList);
        PageHelper.startPage(pageNum, pageSize);

        List<User> studentList = userMapper.selectByExample(example);
        PageInfo<User> pageInfo = new PageInfo<User>(studentList);
        map.put("studentList", studentList);
        map.put("pageInfo", pageInfo);
        return map;
    }


    @Override
    public List<User> findByRnameLike(String rnameLike) {
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUnameLike("%" + rnameLike + "%");


        UserRoleExample example2 = new UserRoleExample();
        com.woniu.tms.entity.UserRoleExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andRidEqualTo(1);
        List<UserRole> list = userRoleMapper.selectByExample(example2);

        List<Integer> uidList = new ArrayList<Integer>();
        for (UserRole integer : list) {
            uidList.add(integer.getUid());
        }


        criteria.andUidIn(uidList);
        List<User> userList = userMapper.selectByExample(example);


        return userList;


    }


    @Override
    public void delete(Integer[] uid) {
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUidIn(Arrays.asList(uid));
        userMapper.deleteByExample(example);
    }


    @Override
    public List<Classes> findClass() {

        return classesMapper.selectByExample(null);

    }


    @Override
    public void studentAdd(User user) {

        userMapper.insertSelective(user);
    }


    @Override
    public List<User> findStudent() {

        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();


        UserRoleExample example2 = new UserRoleExample();

        com.woniu.tms.entity.UserRoleExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andRidEqualTo(1);
        List<UserRole> list = userRoleMapper.selectByExample(example2);

        List<Integer> uidList = new ArrayList<Integer>();
        for (UserRole integer : list) {
            uidList.add(integer.getUid());
        }


        criteria.andUidIn(uidList);
        example.setOrderByClause("uid desc");
        List<User> userList = userMapper.selectByExample(example);


        return userList;
    }


    @Override
    public User findstudent(Integer uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        return user;
    }


    @Override
    public void update(User user) {

        User user2 = userMapper.selectByPrimaryKey(user.getUid());
        user2.setClassid(user.getClassid());
        user2.setUstatus("转班待审核");

        userMapper.updateByPrimaryKey(user2);
    }


    @Override
    public void studentEndow(String sno) {

        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andSnoEqualTo(sno);
        List<User> list = userMapper.selectByExample(example);

        UserRole userRole = new UserRole();
        userRole.setUid(list.get(0).getUid());
        userRole.setRid(1);
        userRoleMapper.insert(userRole);
    }


    @Override
    public void updates(User user) {
        User user2 = userMapper.selectByPrimaryKey(user.getUid());

        user2.setUstatus("未就业");

        userMapper.updateByPrimaryKey(user2);

    }


    @Override
    public Map<String, Object> findByRnameLikeAndUids(String rnameLike, Integer uid, Integer pageNum,
                                                      Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUnameLike("%" + rnameLike + "%");

        ClassesExample classesExample = new ClassesExample();
        com.woniu.tms.entity.ClassesExample.Criteria criteria2 = classesExample.createCriteria();
        criteria2.andClassmanageridEqualTo(uid);

        List<Classes> classesList = classesMapper.selectByExample(classesExample);
        List<Integer> classIdList = new ArrayList<Integer>();
        for (Classes id : classesList) {
            classIdList.add(id.getCid());
        }


        criteria.andClassidIn(classIdList);
        PageHelper.startPage(pageNum, pageSize);

        List<User> studentList = userMapper.selectByExample(example);
        PageInfo<User> pageInfo = new PageInfo<User>(studentList);
        map.put("studentList", studentList);
        map.put("pageInfo", pageInfo);
        return map;

    }


    @Override
    public Map<String, Object> findByRnameLike(String rnameLike, Integer pageNum, Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();
        criteria.andUnameLike("%" + rnameLike + "%");

        List<Classes> classesList = classesMapper.selectByExample(null);
        System.out.println(classesList + "123");
        List<Integer> classesIdList = new ArrayList<Integer>();
        for (Classes integer : classesList) {
            classesIdList.add(integer.getCid());
        }


        criteria.andClassidIn(classesIdList);

        PageHelper.startPage(pageNum, pageSize);

        List<User> studentList = userMapper.selectByExample(example);

        PageInfo<User> pageInfo = new PageInfo<User>(studentList);
        map.put("studentList", studentList);
        map.put("pageInfo", pageInfo);
        return map;
    }

}
